/**
 * 泛型
 */
//普通函数的定义与调用示例
function join(first:string|number,second:string|number){
    let res = `${first}${second}`;
    console.log(res)
    return res;
}
console.log('---------join--------');
join('abc', 'def');
join('abc', 123);

//函数中 - 泛型的使用
function join2<T>(first: T, second: T) {
    let res = `${first}${second}`;
    console.log(res)
    return res;
}
console.log('---------join2--------');
join2<string>('abc','def');
join2<number>(123, 456);
join2<string|number>('abc', 123);

//多个泛型参数
function join3<T, P>(first: T, second: P) {
    let res = `${first}${second}`;
    console.log(res)
    return res;
}
console.log('---------join3--------');
join3<string,string>('abc','def');
join3<string,number>('abc',123);

//泛型-数组的是使用 params: Array<T> 或者 params<T> 两种方法都可以
function join4<T>(params:T[]){
    console.log(params);
    return params;
}
console.log('---------join4--------');
join4<string>(["abc", 'def']);

function join5<T>(params: Array<T>) {
    console.log(params);
    return params;
}
console.log('---------join5--------');
join5<number>([123, 456]);

//泛型 - 类中的应用


//Join6是未使用泛型的类
class Join6 {
    constructor(private items:string[]){}
    getItem(index: number): string {
        return this.items[index];
    }
}
const selectItem = new Join6(["AAA","BBB","CCC"]);
console.log('---------join6--------');
console.log(selectItem.getItem(2));

//Join7是使用泛型的类
class Join7 <T>{
    constructor(private items: T[]) { }
    getItem(index: number): T {
        return this.items[index];
    }
}

console.log('---------join7-si1--------');
const si = new Join7<string>(["AAA", "BBB", "CCC"]);
console.log(si.getItem(1));

const si2 = new Join7<number>([12, 34, 45]);
console.log('---------join7-si2--------');
console.log(si2.getItem(1));

const si3 = new Join7<string|number>([12, 'ABC', 'DEF']);
console.log('---------join7-si3--------');
console.log(si3.getItem(1));

//Join8泛型的约束
class Join8<T extends string|number> {
    constructor(private items: T[]) { }
    getItem(index: number): T {
        return this.items[index];
    }
}

const si4 = new Join8<string>(['AAA','BBB','CCC']);
console.log('---------join8-----------');
console.log(si4.getItem(1));

//泛型中的继承
interface Item{
    name: string;
}

class Join9<T extends Item>{
    constructor(private items: T[]) { }
    getItem(index: number): string {
        return this.items[index].name;
    }
}

const si5 = new Join9<Item>([{ name: "AAA" }, { name: "BBB" }, { name: "CCC" }]);
console.log('---------join9--------');
console.log(si4.getItem(1));